package com.tarena.lbs.basic.service.impl;

import com.tarena.lbs.basic.dao.repository.TagTypeRepository;
import com.tarena.lbs.basic.pojo.param.TagTypeParam;
import com.tarena.lbs.basic.pojo.po.TagTypePO;
import com.tarena.lbs.basic.pojo.vo.TagTypeVO;
import com.tarena.lbs.basic.service.TagTypeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

@Slf4j
@Service
public class TagTypeServiceImpl implements TagTypeService {
    @Autowired
    private TagTypeRepository tagTypeRepository;

    @Override
    public void saveTagType(TagTypeParam param) {
        log.debug("Service:保存标签分类");
        TagTypePO po = assembleBusinessPO(param);
        if(po!=null){
            if(po.getParentId()==null){
                po.setParentId(0);
            }
        }
        po.setStatus(0);
        tagTypeRepository.saveTagType(po);
    }

    @Override
    public void updateType(TagTypeParam param) {
        TagTypePO po = assembleBusinessPO(param);
        tagTypeRepository.updateTagType(po);
    }
    //返回标签分类列表
    @Override
    public List<TagTypeVO> tagTypeList() {
        List<TagTypePO> pos  = tagTypeRepository.tagTypeList();
        //定义所有标签总体数组
        List<TagTypeVO> entities  = new ArrayList<>();
        for(TagTypePO po : pos){
            TagTypeVO vo = new TagTypeVO();
            BeanUtils.copyProperties(po,vo);
            entities.add(vo);
        }
        //定义一级分类的数组
        List<TagTypeVO> level1Menus  = new ArrayList<>();
        // 找到所有的一级分类标签
        for(TagTypeVO vo: entities ){
            if(vo.getParentId()==0){
                level1Menus .add(vo);
            }
        }
        //找到二级分类标签
        for(TagTypeVO vo: level1Menus ){
            vo.setChild(getChildTagTypeList(vo,entities));
        }
        return level1Menus;


    }

    @Override
    public List<TagTypeVO> getTagTypeAll() {
        List<TagTypePO> pos  = tagTypeRepository.tagTypeList();
        List<TagTypeVO> vos  = new ArrayList<>();
        for(TagTypePO po : pos){
            TagTypeVO vo = new TagTypeVO();
            BeanUtils.copyProperties(po,vo);
            vos.add(vo);
        }
        return vos;
    }

    @Override
    public TagTypeVO getTagType(Integer id) {
        TagTypePO po = tagTypeRepository.getTagType(id);
        TagTypeVO vo = new TagTypeVO();
        BeanUtils.copyProperties(po,vo);
        return vo;
    }

    private List<TagTypeVO> getChildTagTypeList(TagTypeVO root,List<TagTypeVO> list){
        List<TagTypeVO> child = new ArrayList<>();
        for(TagTypeVO vo: list){
            if(vo.getParentId().equals(root.getId())){
                vo.setChild(getChildTagTypeList(vo,list));
                child.add(vo);
            }
        }
        //对子类进行排序
        child.sort(new Comparator<TagTypeVO>() {
            @Override
            public int compare(TagTypeVO o1, TagTypeVO o2) {
                return (o1.getId().compareTo(o2.getId()));
            }
        });
        return child;
    }


    /*页面参数转换数据库实体类*/
    private TagTypePO assembleBusinessPO(TagTypeParam param) {
        TagTypePO po = new TagTypePO();
        BeanUtils.copyProperties(param,po);
        return po;
    }
}
